package safe.yunchexing.com.cloudsafe.utils;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Point;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.provider.Telephony;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TelephoneUtil {

    private TelephoneUtil() {

    }

    public static ArrayList<String> getContactsPhones(Context context) {
        ArrayList<String> phoneList = null;

        Cursor cur = null;
        try {
            ContentResolver cr = context.getContentResolver();
            cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            if (cur != null && cur.getCount() > 0) {
                phoneList = new ArrayList<String>();
                while (cur.moveToNext()) {
                    String id = cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    if (Integer
                            .parseInt(cur.getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                        // get the phone number
                        Cursor pCur = cr.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                        + " = ?", new String[]{id}, null);
                        while (pCur.moveToNext()) {
                            String phone = pCur
                                    .getString(pCur
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            phoneList.add(phone);
                        }
                        pCur.close();
                    }
                }
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
        } finally {
            if (cur != null) {
                cur.close();
                cur = null;
            }
        }
        return phoneList;
    }

    /**
     *获取手机型号
     * @return
     */
    public static String getPhoneType(){
        return Build.MODEL;
    }

    public static String getSn(Context context) {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        // device ID
        String sn = tm.getDeviceId();
        if (TextUtils.isEmpty(sn)) {
            // Android ID
            sn = Settings.Secure.getString(context.getContentResolver(),
                    Settings.Secure.ANDROID_ID);
        }
        return sn;
    }

    public static String getPhoneResolution(Context context) {
        WindowManager w = (WindowManager) context.getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        Display d = w.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        d.getMetrics(metrics);
        // since SDK_INT = 1;
        int widthPixels = metrics.widthPixels;
        int heightPixels = metrics.heightPixels;
        // includes window decorations (statusbar bar/menu bar)
        if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
            try {
                widthPixels = (Integer) Display.class.getMethod("getRawWidth")
                        .invoke(d);
                heightPixels = (Integer) Display.class
                        .getMethod("getRawHeight").invoke(d);
            } catch (Exception ignored) {
            }
        }
        // includes window decorations (statusbar bar/menu bar)
        if (Build.VERSION.SDK_INT >= 17) {
            try {
                Point realSize = new Point();
                Display.class.getMethod("getRealSize", Point.class).invoke(d,
                        realSize);
                widthPixels = realSize.x;
                heightPixels = realSize.y;
            } catch (Exception ignored) {
            }
        }
        if (widthPixels > heightPixels) {
            int tmp = widthPixels;
            widthPixels = heightPixels;
            heightPixels = tmp;
        }
        return widthPixels + "x" + heightPixels;
    }

    public static Intent getSmsIntent(Context context, String content) {
        Intent smsIntent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            smsIntent = getNewSmsIntent(context, content);
        } else {
            smsIntent = new Intent(Intent.ACTION_VIEW);
            // smsIntent.setType("vnd.android-dir/mms-sms");
            smsIntent.setData(Uri.parse("sms:"));
            smsIntent.putExtra("sms_body", content);
        }
        return smsIntent;
    }

    @SuppressLint("NewApi")
    private static Intent getNewSmsIntent(Context context, String content) {
        Intent smsIntent;
        String defaultSmsPackageName = Telephony.Sms
                .getDefaultSmsPackage(context);
        smsIntent = new Intent(Intent.ACTION_SEND);
        smsIntent.setType("text/plain");
        smsIntent.putExtra(Intent.EXTRA_TEXT, content);
        if (defaultSmsPackageName != null) {
            smsIntent.setPackage(defaultSmsPackageName);
        }
        return smsIntent;
    }

    /**
     * 匹配器
     * @param numPattern 正则表达式模板
     * @param phoneNum  要匹配的数据
     * @return
     */
    private static boolean isMatcher(String numPattern,String phoneNum){
        //匹配正则
        Pattern p = Pattern.compile(numPattern);
        Matcher m = p.matcher(phoneNum);
        return m.matches();
    }
}
